package com.cnbbx.cnbbx_web.service.impl;

import com.alibaba.fastjson2.JSON;
import com.cnbbx.cnbbx_web.domain.UserInfo;
import com.cnbbx.cnbbx_web.mapper.UserInfoMapper;
import com.cnbbx.cnbbx_web.service.UserInfoService;
import com.cnbbx.mybatis.mysql.AbstractMySqlBaseDao;
import com.cnbbx.mybatis.query.builder.InsertBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

@Service
@Slf4j
public class UserInfoServiceImpl extends AbstractMySqlBaseDao<UserInfo> implements UserInfoService {
    @Override
    public Class mapperClass() {
        return UserInfoMapper.class;
    }

    public UserInfo generateRandomUser() {
        Random random = new Random();
        String name = "user" + random.nextInt(10000);
        String phone = "138" + String.format("%08d", random.nextInt(99999999));
        return UserInfo.builder().uName(name).uPhone(phone).build();
    }

    /**
     * 每隔30秒新增用户
     */
    public void addUserPeriodically() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                UserInfo user = generateRandomUser();
                InsertBuilder insert = InsertBuilder.build(UserInfo.class).set(user);
                insert(insert);
            }
        }, 0, 30000);
    }
}
